package com.education.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Fan307 on 2016/11/8.
 */
public class DateUtil {

    private static final String TIME_FORMAT_DEC = "yyyy-MM-dd HH:mm:ss";
    private static final String TIME_FORMAT_MIN = "yyyy-MM-dd HH:mm";

    public static final String TIME_FORMAT_1= "MMdd";
    public static final String TIME_FORMAT_2= "yyyy-MM-dd";
    public static final String TIME_FORMAT_3= "yyyy年MM月dd日";
    public static final String TIME_FORMAT_4= "yyyy年MM月dd日 HH:mm";


    public static String getDateString(long time) {
        return formatDate(time,TIME_FORMAT_DEC);
    }


    public static String fomateDate(Date date, String pattern){
        SimpleDateFormat afterFormat = new SimpleDateFormat(pattern, Locale.CHINA);
        return afterFormat.format(date);
    }

    public static String formatDate(String dateStr, String beforePattern, String afterPattern) {
        SimpleDateFormat format = new SimpleDateFormat(beforePattern, Locale.CHINA);
        Date date = null;
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        SimpleDateFormat afterFormat = new SimpleDateFormat(afterPattern, Locale.CHINA);


        return afterFormat.format(date);
    }

    public static String formatDate(long time,String pattern) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.CHINA);
        return format.format(c.getTime());
    }

    public static Date getTime(String souceTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_MIN);
        return sdf.parse(souceTime);
    }

    public static long getDateNoTime() {
        Calendar calendar = Calendar.getInstance();
        return getDateNoTime(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    }

    public static long getDateNoTime(int year, int month, int dayOfMounth) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.MONTH,month);
        calendar.set(Calendar.DAY_OF_MONTH ,dayOfMounth);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTimeInMillis();
    }
}
